package rsi.mesoscopic.view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;

public class LineArrow {
	
	private float arrowLength;
	
	public LineArrow(float al) {
		arrowLength = al;
	}
	
    public void draw (Graphics2D g)
    {
        // where the control point for the intersection of the V needs calculating
        // by projecting where the ends meet

        float arrowRatio = 0.5f;

        BasicStroke stroke = ( BasicStroke ) g.getStroke();

        float endX = 350.0f;

        float veeX;

        switch ( stroke.getLineJoin() ) {
            case BasicStroke.JOIN_BEVEL:
                // IIRC, bevel varies system to system, this is approximate
                veeX = endX - stroke.getLineWidth() * 0.25f;
                break;
            default:
            case BasicStroke.JOIN_MITER:
                veeX = endX - stroke.getLineWidth() * 0.5f / arrowRatio;
                break;
            case BasicStroke.JOIN_ROUND:
                veeX = endX - stroke.getLineWidth() * 0.5f;
                break;
        }

        // vee
        Path2D.Float path = new Path2D.Float();

        path.moveTo ( veeX - arrowLength, -arrowRatio*arrowLength );
        path.lineTo ( veeX, 0.0f );
        path.lineTo ( veeX - arrowLength, arrowRatio*arrowLength );

        g.setColor ( Color.BLUE );
        g.draw ( path );

        // stem for exposition only
        g.setColor ( Color.YELLOW );
        g.draw ( new Line2D.Float ( 50.0f, 0.0f, veeX, 0.0f ) );

        // in practice, move stem back a bit as rounding errors
        // can make it poke through the sides of the Vee
        g.setColor ( Color.RED );
        g.draw ( new Line2D.Float ( 50.0f, 0.0f, veeX - stroke.getLineWidth() * 0.25f, 0.0f ) );
    }
}
